#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_NoticeOfAmendmentOfImportManifestSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'NoticeOfAmendmentOfImportManifest'
    
    #Read from inn by XML order

    #NoticeOfAmendmentOfImportManifest  
    IntegratedAmendmentTypeCode = inn.get({'BOTSID':rootelement,ns1+'IntegratedAmendmentTypeCode':None})
    
    #NoticeOfAmendmentOfImportManifest/Document
    DocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':None})
    DocumentIssueDateTime = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDateTime':None})
    
    #NoticeOfAmendmentOfImportManifest/UCR
    UCRIdentificationID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'IdentificationID':None})
    
    out.ta_info['botskey'] = 'CUSMAN-5FF-' + UCRIdentificationID.strip()
    
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSMAN'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'KE'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':DocumentTypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':UCRIdentificationID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':IntegratedAmendmentTypeCode,'C529.1131':'5CM','C529.3055':'KCS'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'137'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2380':utils.edifactYYYYMMDDHHNN(DocumentIssueDateTime)})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2379':'203'})

    #NoticeOfAmendmentOfImportManifest/AmendmentContent
    for dms in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'AmendmentContent'}):
        AmendmentContentSequenceID = dms.get({'BOTSID':ns1+'AmendmentContent',ns2+'SequenceID':None})
        AmendmentContentLineNumberID = dms.get({'BOTSID':ns1+'AmendmentContent',ns2+'LineNumberID':None})
        AmendmentContentBeforeDescription = dms.get({'BOTSID':ns1+'AmendmentContent',ns2+'BeforeDescription':None})
        AmendmentContentAfterDescription = dms.get({'BOTSID':ns1+'AmendmentContent',ns2+'AfterDescription':None})
        
        lou = out.putloop({'BOTSID':'UNH'},{'BOTSID':'DMS'})
        lou.put({'BOTSID':'DMS','7240':int(AmendmentContentSequenceID)})
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':AmendmentContentLineNumberID})
        if AmendmentContentBeforeDescription != None:
            lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':AmendmentContentLineNumberID,'C108.4440#2':AmendmentContentBeforeDescription[0:50]})
            lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':AmendmentContentLineNumberID,'C108.4440#2':AmendmentContentBeforeDescription[0:50],'C108.4440#3':AmendmentContentBeforeDescription[50:100]})
            lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':AmendmentContentLineNumberID,'C108.4440#2':AmendmentContentBeforeDescription[0:50],'C108.4440#3':AmendmentContentBeforeDescription[50:100],'C108.4440#4':AmendmentContentBeforeDescription[100:150]})
            lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':AmendmentContentLineNumberID,'C108.4440#2':AmendmentContentBeforeDescription[0:50],'C108.4440#3':AmendmentContentBeforeDescription[50:100],'C108.4440#4':AmendmentContentBeforeDescription[100:150],'C108.4440#5':AmendmentContentBeforeDescription[150:200]})
        
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'ACB','C108.4440#1':AmendmentContentAfterDescription[0:50]})
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'ACB','C108.4440#1':AmendmentContentAfterDescription[0:50],'C108.4440#2':AmendmentContentAfterDescription[50:100]})
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'ACB','C108.4440#1':AmendmentContentAfterDescription[0:50],'C108.4440#2':AmendmentContentAfterDescription[50:100],'C108.4440#3':AmendmentContentAfterDescription[100:150]})
        lou.put({'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'ACB','C108.4440#1':AmendmentContentAfterDescription[0:50],'C108.4440#2':AmendmentContentAfterDescription[50:100],'C108.4440#3':AmendmentContentAfterDescription[100:150],'C108.4440#4':AmendmentContentAfterDescription[150:200]})
    # End Loop    
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
    
    
#    AmendmentContentSequenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'AmendmentContent',ns2+'SequenceID':None})
#    AmendmentContentLineNumberID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'AmendmentContent',ns2+'LineNumberID':None})
#
#    out.put({'BOTSID':'UNH'},{'BOTSID':'DMS','7240':AmendmentContentSequenceID})
#    out.put({'BOTSID':'UNH'},{'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':AmendmentContentLineNumberID})
#    num = 1
#    for ftx in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'AmendmentContent'},{'BOTSID':ns2+'BeforeDescription'}):
#        AmendmentContentBeforeDescription = ftx.get({'BOTSID':ns2+'BeforeDescription','BOTSCONTENT':None})
#        num = num + 1    
#        out.put({'BOTSID':'UNH'},{'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'CHG','C108.4440#{0}'.format(num):AmendmentContentBeforeDescription[0:50]})
#
#    num = 0        
#    for ftx in inn.getloop({'BOTSID':rootelement},{'BOTSID':ns1+'AmendmentContent'},{'BOTSID':ns2+'AfterDescription'}):
#        AmendmentContentAfterDescription = ftx.get({'BOTSID':ns2+'AfterDescription','BOTSCONTENT':None})
#        num = num + 1     
#        out.put({'BOTSID':'UNH'},{'BOTSID':'DMS'},{'BOTSID':'FTX','4451':'ACB','C108.4440#{0}'.format(num):AmendmentContentAfterDescription[0:50]})
#            
#    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
#    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
